package com.gitee.resource;

import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;

import org.springframework.context.i18n.LocaleContextHolder;

import com.google.common.collect.Maps;

/**
 * 读取资源配置文件
 * 
 * @author 赵炎
 * @version [V1.00, 2019年1月24日]
 * @since V1.00
 */
public class Resources
{
    private static final Map<String, ResourceBundle> MESSAGES = Maps.newHashMap();
    
    /**
     * 获取信息
     * 
     * @param key
     * @param params
     * @return
     */
    public static String getMessage(String key, Object... params)
    {
        Locale locale = LocaleContextHolder.getLocale();
        ResourceBundle message = MESSAGES.get(key);
        if (message == null)
        {
            synchronized (MESSAGES)
            {
                message = MESSAGES.get(locale.getLanguage());
                if (message == null)
                {
                    message = ResourceBundle.getBundle("i18n/messages", locale);
                    MESSAGES.put(locale.getLanguage(), message);
                }
            }
        }
        if (params != null && params.length > 0)
        {
            return String.format(message.getString(key), params);
        }
        return message.getString(key);
    }
    
    /**
     * 清除国际化信息
     * 
     */
    public static void flushMessage()
    {
        MESSAGES.clear();
    }
    
}
